﻿
using Dapper;
using Dapper.Contrib.Extensions;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using System.Threading.Tasks;
using Wittur.GenereateKongCoCPDF.Core.Common;

namespace Wittur.GenereateKongCoCPDF.Core.DB;
public class DbContentHelper<T> where T : class
{
    private readonly string _connectionString;
    private static readonly Lazy<DbContentHelper<T>> _instance =
        new Lazy<DbContentHelper<T>>(() => new DbContentHelper<T>());

    public static DbContentHelper<T> Instance => _instance.Value;

    private DbContentHelper()
    {
        _connectionString = ConfigHelper.Instance.GetString("Database", "ConnectionString");
    }

    /// <summary>
    /// 通过sql获取单条数据
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<IEnumerable<T>> GetAllBySql(string sql, object param)
    {
        using (var db = new OracleConnection(_connectionString))
        {
            db.Open();
            return await db.QueryAsync<T>(sql, param);
        }
    }

    /// <summary>
    /// 通过sql获取单条数据
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<T> GetTBySqlAsync(string sql, object param)
    {
        using (var db = new OracleConnection(_connectionString))
        {
            db.Open();
            return await db.QueryFirstOrDefaultAsync<T>(sql, param);
        }
    }

    // 创建（插入）记录
    public long Insert(T entity)
    {
        using (var db = new OracleConnection(_connectionString))
        {
            db.Open();
            return db.Insert(entity);
        }
    }

    // 读取（获取）所有记录
    public IEnumerable<T> GetAll()
    {
        try
        {
            using (var db = new OracleConnection(_connectionString))
            {
                db.Open();
                return db.GetAll<T>();
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException("GetAll operation failed", ex);
        }
    }


    // 读取（获取）单个记录
    public T GetById(object id)
    {
        try
        {
            using (var db = new OracleConnection(_connectionString))
            {
             
                db.Open();
                return db.Get<T>(id);
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException("GetById operation failed", ex);
        }
    }

    // 更新记录
    public bool Update(T entity)
    {
        try
        {
            using (var db = new OracleConnection(_connectionString))
            {
                db.Open();
                return db.Update(entity);
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException("Update operation failed", ex);
        }
    }

    // 删除记录
    public bool Delete(T entity)
    {
        try
        {
            using (var db = new OracleConnection(_connectionString))
            {
                db.Open();
                return db.Delete(entity);
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException("Delete operation failed", ex);
        }
    }

    // 根据 ID 删除记录
    public bool DeleteById(object id)
    {
        try
        {
            using (var db = new OracleConnection(_connectionString))
            {
                db.Open();
                var entity = GetById(id);
                return entity != null && Delete(entity);
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException("DeleteById operation failed", ex);
        }
    }
}
